#!/usr/bin/env python

class MantuxCmd ( object ):
	def __init__ ( self, mantux ):
		self.mantux = mantux

	def go ( self ):
		import argparse

		parser = argparse.ArgumentParser ( description = 'Mantux project mantainer' )

		parser.add_argument ( '-p', '--path', action = 'append', help = 'Additional repository configurations' )
		parser.add_argument ( '-v', '--version', action = 'version', version = '0.0.1' )
		parser.add_argument ( '--debug', action = 'store_true' )
		#parser.add_argument ( 'command', choices = commands, help = 'Available commands' )
		#parser.add_argument ( 'args', nargs = '*' )

		self._init_subparsers ( parser )

		args = vars ( parser.parse_args () )

		res = {}
		for k,v in args.iteritems ():
			if v == None: continue
			res [ k ] = v

		#print res

		if 'func' in res:
			args [ 'func' ] ( self.mantux, res )

	def _init_subparsers ( self, parser ):
		commands = self.mantux.commands

		subp = parser.add_subparsers ( title = "Valid commands", metavar = "" )

		for cmd in commands.values ():
			p = subp.add_parser ( cmd [ 'name' ], help = cmd.get ( "help", "" ) )
			p.set_defaults ( func = cmd [ 'func' ] )
			if cmd.get ( "params" ):
				for a in cmd [ 'params' ]:
					p.add_argument ( a [ 0 ], ** a [ 1 ] )
